<?php

class IndexController extends Zend_Controller_Action {

    public function indexAction() {
		
    	$this->view->action = 'index';
    	$this->view->language = Zend_Registry::get('session')->lang;
    }
    
    public function catalogueAction()
    {
    	$categories = new Application_Model_Categorie();
    	$categories = $categories->fetchAlldata();
    	$this->view->categories = $categories;
    	 
    	$this->view->action = 'catalogue';
    	$this->view->language = Zend_Registry::get('session')->lang;
    }
    
    public function listeproduitsAction()
    {
    	$categorieid = $this->getRequest()->getParam('categorieid');
    	$articles = new Application_Model_Article();
    	$articles = $articles->fetchPhotoAndIdByCategorie($categorieid);
    	$this->view->articles = $articles;
    	
    	$this->view->action = 'listeproduits';
    	$this->view->language = Zend_Registry::get('session')->lang;
    }
    
    public function produitAction()
    {
    	$this->view->action = 'produit';
    	$this->view->language = Zend_Registry::get('session')->lang;
    }
    
    public function couleursAction()
    {
    	$idarticle = $this->getRequest()->getParam("idarticle");
    	$couleurs = new Application_Model_Couleur();
    	$couleurs = $couleurs->fetchByArticle($idarticle);
    
    	if($this->getRequest()->isXmlHttpRequest())
    	{
    		$this->_helper->layout()->disableLayout();
    		$this->view->couleurs = $couleurs;
    	}
    }
    
    public function articleAction()
    {
    	sleep(1);
    	$idarticle = $this->getRequest()->getParam('articleid');
    	$article = new Application_Model_Article();
    	$article = $article->fetchByArticle($idarticle);
    	if($this->getRequest()->isXmlHttpRequest())
    	{
    		$this->_helper->layout()->disableLayout();
    		$this->view->article = $article;
    		
    		$couleurs = new Application_Model_Couleur();
    		$couleurs = $couleurs->fetchByArticle($idarticle);
    		$this->view->couleurs = $couleurs;
    	}
    }
    
    public function getphotoAction()
    {
    	sleep(1);
    	if($this->getRequest()->isXmlHttpRequest())
    	{
    		$this->_helper->layout()->disableLayout();
    		$this->_helper->viewRenderer->setNoRender(true);
    		$idarticle = $this->getRequest()->getParam("idarticle");
    		$idcouleur = $this->getRequest()->getParam("idcouleur");
    
    		$acommecouleur = new Application_Model_ACommeCouleur();
    		$acommecouleur = $acommecouleur->fetchByCouleurAndArticle($idarticle, $idcouleur);
    
    		$data = array(
    				"photo"=> $acommecouleur['ARTICLE_PHOTO']
    		);
    
    		$this->_helper->json($data);
    	}
    }
    
    
	public function getcountpostAction()
	{
		if($this->getRequest()->isXmlHttpRequest())
    	{
			$posts = new Application_Model_Post();
			$recherche = $this->getRequest()->getParam("recherche");
			$nbtotalpost = $posts->countAllPost($recherche);
			$nbtotalpost = $nbtotalpost['count'];

			$this->_helper->layout()->disableLayout();
			$this->_helper->viewRenderer->setNoRender(true);
			$data = array(
    			"nbtotalpost"=> $nbtotalpost
				);
				
			$this->_helper->json($data);
		}
	}
	
    public function actualiteAction() {
    	
    	$this->view->action = 'actualite';
    	$this->view->language = Zend_Registry::get('session')->lang;
    	
		$posts = new Application_Model_Post();
		$nbpostparpage = 3;
		
		//Requete Ajax
		if($this->getRequest()->isXmlHttpRequest())
    	{
				$pageactuelle = $this->getRequest()->getParam('pagesuivante');
				$begin = $this->getRequest()->getParam('begin');
				$recherche = $this->getRequest()->getParam('recherche');
				
    			$this->_helper->layout()->disableLayout();
				$posts = $posts->fetchByValeur($nbpostparpage,$begin, $recherche);
				$ecart = count($posts);
		}
		else
		{
				$pageactuelle = 1;
				$begin = 0;
				$posts = $posts->fetchLimitPostOrderById($nbpostparpage,$begin);
				$ecart = count($posts);
		}
		$this->view->begin = $begin;
		$this->view->ecart = $ecart;
	
		$this->view->nbpostparpage = $nbpostparpage;
		$this->view->pageactuelle = $pageactuelle;
		$this->view->posts = $posts;
    }
    
    public function contactAction() {
    
    	$this->view->action = 'contact';
    	$this->view->language = Zend_Registry::get('session')->lang;
    	
    	$form = new Application_Form_Contact();
    	$this->view->form = $form;
    }
	
    
    // Fonction pour changer de langage
    public function languageAction(){
    	//On récupère les données de la requête
    	$data =$this->getRequest()->getParams();
    	
    	if(isset($data['lang']) && in_array($data['lang'], array('en','fr'))){
    		Zend_Registry::get('session')->lang = $data['lang'];
    	}
    	
    	//On redirige sur la page actuelle avec la nouvelle langue
    	$this->_redirect('/index/'.$data['actionname']);
    }
}
